package com.tian.photopicker.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

import com.tian.photopicker.R;
import com.tian.photopicker.adapter.PickerGridAdapter;
import com.tian.photopicker.bean.BundleBean;

import static com.tian.photopicker.PhotoPicker.IS_CROP;
import static com.tian.photopicker.PhotoPicker.IS_SHOW_CAMERA;
import static com.tian.photopicker.PhotoPicker.IS_USE_HEAD;
import static com.tian.photopicker.PhotoPicker.RECYCLER_VIEW_COLUMNS;
import static com.tian.photopicker.PhotoPicker.SELECT_PHOTO_COUNT;

/**
 * 图片选择界面
 * @author tian
 */
public class PhotoPickerActivity extends AppCompatActivity {
    /**
     * 默认列数
     */
    public static final int DEFAULT_COLUMNS = 3;
    /**
     * 默认最大选取数
     */
    public static final int DEFAULT_MAX_SELECT_COUNT = 9;
    private int column = DEFAULT_COLUMNS;
    private int selectCount = DEFAULT_MAX_SELECT_COUNT;
    private static final String FRAGMENT_TAG= "fragment_tag";
    private PhotoPickerFragment photoPickerFragment;
    private PhotoPreviewFragment photoPreviewFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        Intent intent = getIntent();
        boolean isShowCamera = intent.getBooleanExtra(IS_SHOW_CAMERA,true);
        column = intent.getIntExtra(RECYCLER_VIEW_COLUMNS,DEFAULT_COLUMNS);
        selectCount = intent.getIntExtra(SELECT_PHOTO_COUNT,DEFAULT_MAX_SELECT_COUNT);
        boolean useHead = intent.getBooleanExtra(IS_USE_HEAD,false);
        boolean isCrop = intent.getBooleanExtra(IS_CROP,false);
        if (useHead) {
            selectCount = 1;
        }
        if (isCrop) {
            selectCount = 1;
        }
        BundleBean bean = new BundleBean();
        bean.setShowCamera(isShowCamera);
        bean.setColumn(column);
        bean.setSelectCount(selectCount);
        bean.setUseHead(useHead);
        bean.setCrop(isCrop);
        setContentView(R.layout.activity_photo_picker);
        photoPickerFragment = (PhotoPickerFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
        if (photoPickerFragment == null) {
            photoPickerFragment = PhotoPickerFragment
                    .newInstance(bean);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container,photoPickerFragment,FRAGMENT_TAG)
                    .commit();
            getSupportFragmentManager().executePendingTransactions();
        }

    }

    /**
     * 切换
     * @param photoPreviewFragment
     */
    public void addPreviewImageFragment(PhotoPreviewFragment photoPreviewFragment) {
        this.photoPreviewFragment = photoPreviewFragment;
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.replace(R.id.fragment_container, this.photoPreviewFragment)
                .addToBackStack(null)
                .commit();
    }
    public PickerGridAdapter getGridAdapter () {
        return photoPickerFragment.getPickerGridAdapter();
    }

}
